<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">
    <!-- Google Chrome Frame也可以让IE用上Chrome的引擎: -->
    <meta name="renderer" content="webkit">
    <!--国产浏览器高速模式-->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>作业管理系统</title>

    <!-- 公共样式 开始 -->
    <link rel="stylesheet" type="text/css" href="../../css/base.css">
    <link rel="stylesheet" type="text/css" href="../../css/iconfont.css">
    <script type="text/javascript" src="../../framework/jquery-1.11.3.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../../layui/css/layui.css">
    <script type="text/javascript" src="../../layui/layui.js"></script>

    <!-- 滚动条插件 -->
    <link rel="stylesheet" type="text/css" href="../../css/jquery.mCustomScrollbar.css">
    <script src="../../framework/jquery-ui-1.10.4.min.js"></script>
    <script src="../../framework/jquery.mousewheel.min.js"></script>
    <script src="../../framework/jquery.mCustomScrollbar.min.js"></script>
    <script src="../../framework/cframe.js"></script><!-- 仅供所有子页面使用 -->
    <!-- 公共样式 结束 -->
    <script type="text/javascript" src="../../framework/frameAll.js"></script>
</head>
<body>
<div class="cBody">
    <div class="console">
        <!-- form搜索框or添加框 -->
        <form class="layui-form">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" name="name" required placeholder="请输入需要检索的信息"
                           autocomplete="off" class="layui-input">
                </div>
                <button class="layui-btn" lay-submit lay-filter="formDemo">检索</button>
                <button id="addClass" class="layui-btn layui-btn-warm" style="float: right;" type="button">添加</button>
            </div>
        </form>
        <!-- 表格 -->
        <table class="layui-table-cell layui-hide" id="demo" lay-filter="test"></table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="detail">查看</a>
            <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="del">删除</a>
        </script>
        <!-- layUI 分页模块 -->
        <div id="pages"></div>
    </div>
</div>
<script>
    layui.use(['table', 'form'], function () {
        var table = layui.table,
        form = layui.form;

        var content = "";

        var tableIns = table.render({
            elem: '#demo',
            height: "full-200",
            method: "get",
            request: {
                pageName: 'pageNo',//页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            where: {
                action: "page",
                content: content
            },
            url: '/ClassServlet.do',//数据接口
            page: true, //开启分页
            cellMinWidth: 100,
            limits: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
            cols: [[ //表头
                {title: '序号', type: 'numbers'},
                {field: 'name', title: '班级名称'},
                {field: 'number', title: '学生人数', sort: true},
                {title: '操作', align: 'center', width: 200, toolbar: '#barDemo', fixed: 'right'}
            ]]
        });
        //查询
        form.on('submit(formDemo)', function (data) {
            content = data.field.name;
            tableIns.reload({
                where: {content: content},
                page: {curr: 1}
            });
            return false;
        });
        //添加
        $("#addClass").click(function () {
            layer.prompt({
                    formType: 3,
                    title: '添加班级名称：',
                    offset: '100px'
                },
                function (value, index, elem) {
                    $.ajax({
                        type: "get",
                        url: "/ClassServlet.do",
                        data: {
                            action: "add",
                            name: value
                        },
                        dataType: "text",
                        success: function (text) {
                            var tip = parseInt(text);
                            if (tip == 0) {
                                tableIns.reload({
                                    where: {content: content},
                                    page: {curr: $(".layui-laypage-skip .layui-input").val()}
                                });
                                yesMsg('添加成功！');
                                layer.close(index);
                            } else {
                                noMsg('班级名称重复！');
                            }
                        }
                    });
                });
        });
        //编辑与删除
        table.on('tool(test)', function (obj) {
            //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if (layEvent === 'detail') {
                location.href = 'addstuagent_list.html?content=' + data.name;
            } else if (layEvent === 'del') { //删除
                layer.confirm('是否删除' + data.name, {
                    offset: '100px',
                    icon: 3
                }, function (index) {
                    //向服务端发送删除指令
                    $.ajax({
                        type: "get",
                        url: "/ClassServlet.do",
                        data: {
                            action: "del",
                            id: data.id
                        },
                        dataType: "text",
                        success: function (text) {
                            var tip = parseInt(text);
                            if (tip == 0) {
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                yesMsg("删除成功！");
                            } else {
                                noMsg("删除失败！");
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (layEvent === 'edit') { //编辑
                //do something
                layer.prompt({
                    formType: 3,
                    title: '编辑班级名称：',
                    value: data.name,
                    offset: '100px'
                }, function (value, index, elem) {
                    $.ajax({
                        type: "get",
                        url: "/ClassServlet.do",
                        data: {
                            action: "update",
                            id: data.id,
                            name: value
                        },
                        dataType: "text",
                        success: function (text) {
                            var tip = parseInt(text);
                            if (tip === 0) {
                                obj.update({
                                    name: value,
                                });
                                yesMsg('更新成功！');
                            } else {
                                noMsg('更新失败！');
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>

</html>
